var autodb = require('../../db/auto.db')
var orderdb = require('../../db/order.db');
var async = require('async');
var emit = require('../../emit');

function onsocket(socket, data) {
    var user = data.user;
    async.waterfall([
        (callback) => {
            orderdb.getorder(user, data.orderid, (err, response) => {
                callback(err, response);
            })
        },
        (order, callback) => {
            var infos = [];
            async.forEach(order.ids, (id, cb) => {
                autodb.getfileinfo(true, id, (err, info) => {
                    if (err || info == null) {
                        cb(null);
                    } else {
                        infos.push(info);
                        cb(null);
                    }
                })
            }, (err) => {
                callback(err, order, infos);
            })
        }
    ], (err, order, infos) => {
        if (err) {
            emit.send(socket, 'orderdetail', null);
        } else {
            order.orderid = data.orderid;
            order.infos = infos;
            emit.send(socket, 'orderdetail', order);
        }
    });
}

module.exports = {
    onsocket
}